home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 07 - 1991 / 07.06 Jun 91 / Fancy Dialogs Code / TDialog.p < prev    next >
Encoding:
Text File  |  1991-03-18  |  19.4 KB  |  1,013 lines  |  [TEXT/PJMM]

  1. { Methods for TDialog. }
  2.  
  3. { Written by Thomas Engel, M.D. }
  4. { Copyright © 1991 MacTutor. }
  5.  
  6.  
  7. unit TDialog;
  8.  
  9. interface
  10.  
  11.     uses
  12.         SANE, Globals;
  13.  
  14.  
  15.     function DialogFilter (toolboxDialog: DialogPtr; var theEvent: EventRecord; var itemHit: Integer): Boolean;
  16.  
  17.  
  18. implementation
  19.  
  20.  
  21.     function DialogFilter (toolboxDialog: DialogPtr; var theEvent: EventRecord; var itemHit: Integer): Boolean;
  22.  
  23.     { Dialog filter function. }
  24.  
  25.         var
  26.             theDialog: TDialog;
  27.  
  28.     begin
  29.         theDialog := TDialog(DialogPeek(toolboxDialog)^.window.refCon);
  30.         DialogFilter := theDialog.EventFilter(theEvent, itemHit)
  31.     end;
  32.  
  33.  
  34.     procedure TDialog.IDialog (dialogID: Integer; default, cancel, text: Integer; dismiss: DialogItemSet; centered: Boolean);
  35.  
  36.     { Initialize dialog. }
  37.  
  38.     begin
  39.         defaultItem := default;
  40.         cancelItem := cancel;
  41.         defaultText := text;
  42.         dismissSet := dismiss;
  43.         itemCount := 0;
  44.         toolboxDialog := GetNewDialog(dialogID, nil, pointer(-1));
  45.         toolboxDPeek := DialogPeek(toolboxDialog);
  46.         if toolboxDialog <> nil then
  47.             begin
  48.                 toolboxDPeek^.window.refCon := Longint(self);
  49.                 itemCount := IntegerHandle(toolboxDPeek^.items)^^ + 1
  50.             end;
  51.         if centered then
  52.             Center
  53.     end;
  54.  
  55.  
  56.     procedure TDialog.Free;
  57.  
  58.     { Free memory used by this object. }
  59.  
  60.     begin
  61.         if toolboxDialog <> nil then
  62.             DisposDialog(toolboxDialog);
  63.         HUnlock(Handle(self));
  64.         DisposHandle(Handle(self))
  65.     end;
  66.  
  67.  
  68.     procedure TDialog.Center;
  69.  
  70.     { Center the dialog on the main screen. }
  71.  
  72.         var
  73.             width, height: Integer;
  74.  
  75.     begin
  76.         if toolboxDialog <> nil then
  77.             begin
  78.                 with toolboxDialog^.portRect do
  79.                     begin
  80.                         width := right - left;
  81.                         height := bottom - top
  82.                     end;
  83.                 with ScreenBits.bounds do
  84.                     MoveWindow(toolboxDialog, left + (right - left - width) div 2, top + (bottom - top - height) div 3, false)
  85.             end
  86.     end;
  87.  
  88.  
  89.     function TDialog.Show: Integer;
  90.  
  91.     { Show the dialog. }
  92.  
  93.         var
  94.             itemHit: Integer;
  95.  
  96.     begin
  97.         Show := 0;
  98.         if toolboxDialog <> nil then
  99.             begin
  100.                 if defaultText > 0 then
  101.                     SelIText(toolboxDialog, defaultText, 0, 32767);
  102.                 ShowWindow(toolboxDialog);
  103.                 InitCursor;
  104.                 repeat
  105.                     ModalDialog(@DialogFilter, itemHit);
  106.                     if itemHit > 0 then
  107.                         Hit(itemHit);
  108.                 until (itemHit > 0) and (itemHit in dismissSet);
  109.                 Show := itemHit
  110.             end
  111.     end;
  112.  
  113.  
  114.     procedure TDialog.Hide;
  115.  
  116.     { Hide the dialog. }
  117.  
  118.     begin
  119.         if toolboxDialog <> nil then
  120.             HideWindow(toolboxDialog);
  121.     end;
  122.  
  123.  
  124.     function TDialog.EventFilter (var theEvent: EventRecord; var itemHit: Integer): Boolean;
  125.  
  126.     { Dialog event filter. }
  127.  
  128.         var
  129.             savePort: GrafPtr;
  130.             ch: Char;
  131.             itemType: Integer;
  132.             itemHandle: Handle;
  133.             itemRect: Rect;
  134.             editItem: Integer;
  135.  
  136.     begin
  137.  
  138.         { Save current port. }
  139.  
  140.         GetPort(savePort);
  141.         SetPort(toolboxDialog);
  142.  
  143.         { Fix cursor. }
  144.  
  145.         FixCursor;
  146.  
  147.          { Check the event. }
  148.  
  149.         case theEvent.what of
  150.  
  151.             keyDown, autoKey: 
  152.                 begin
  153.                     ch := Chr(BitAnd(theEvent.message, CharCodeMask));
  154.  
  155.                    { Handle default item. }
  156.  
  157.                     if (ch in [ReturnCh, EnterCh]) and (defaultItem > 0) then
  158.                         begin
  159.                             FlashItem(defaultItem);
  160.                             itemHit := defaultItem;
  161.                             EventFilter := true
  162.                         end
  163.  
  164.                     { Handle cancel item. }
  165.  
  166.                     else if (BitAnd(theEvent.modifiers, CmdKey) <> 0) and (ch in ['.']) and (cancelItem > 0) then
  167.                         begin
  168.                             FlashItem(cancelItem);
  169.                             itemHit := cancelItem;
  170.                             EventFilter := true
  171.                         end
  172.  
  173.                     { Handle tab key. }
  174.  
  175.                     else if (ch in [TabCh]) and (toolboxDPeek^.editField <> -1) then
  176.                         begin
  177.                             if BitAnd(theEvent.modifiers, ShiftKey) <> 0 then
  178.                                 begin
  179.  
  180.                                     { Find previous edit item. }
  181.  
  182.                                     editItem := toolboxDPeek^.editField + 1;
  183.                                     repeat
  184.                                         if editItem = 1 then
  185.                                             editItem := itemCount
  186.                                         else
  187.                                             editItem := editItem - 1;
  188.                                         GetDItem(toolboxDialog, editItem, itemType, itemHandle, itemRect);
  189.                                     until itemType = editText;
  190.                                 end
  191.                             else
  192.                                 begin
  193.  
  194.                                     { Find next edit item. }
  195.  
  196.                                     editItem := toolboxDPeek^.editField + 1;
  197.                                     repeat
  198.                                         if editItem = itemCount then
  199.                                             editItem := 1
  200.                                         else
  201.                                             editItem := editItem + 1;
  202.                                         GetDItem(toolboxDialog, editItem, itemType, itemHandle, itemRect);
  203.                                     until itemType = editText
  204.                                 end;
  205.  
  206.                             { Select the edit field. }
  207.  
  208.                             SelIText(toolboxDialog, editItem, 0, 32767);
  209.                             itemHit := editItem;
  210.                             EventFilter := true
  211.                         end
  212.  
  213.                     { Handle keyboard commands. }
  214.  
  215.                     else if BitAnd(theEvent.modifiers, CmdKey) <> 0 then
  216.                         begin
  217.                             Command(ch, itemHit);
  218.                             EventFilter := true
  219.                         end
  220.  
  221.                     { Handle other typing. }
  222.  
  223.                     else
  224.                         EventFilter := false
  225.                 end;
  226.  
  227.             updateEvt: 
  228.                 begin
  229.                     BeginUpdate(toolboxDialog);
  230.                     UpdtDialog(toolboxDialog, toolboxDialog^.visRgn);
  231.                     Draw;
  232.                     EndUpdate(toolboxDialog);
  233.                     EventFilter := true
  234.                 end;
  235.  
  236.             otherwise
  237.                 EventFilter := false
  238.         end;
  239.  
  240.         { Restore port. }
  241.  
  242.         SetPort(savePort)
  243.     end;
  244.  
  245.  
  246.     procedure TDialog.FixCursor;
  247.  
  248.     { Fix the cursor. }
  249.  
  250.         var
  251.             localMouse: Point;
  252.             itemType: Integer;
  253.             itemHandle: Handle;
  254.             itemRect: Rect;
  255.  
  256.     begin
  257.         if toolboxDPeek^.editField <> -1 then
  258.             begin
  259.                 GetMouse(localMouse);
  260.                 GetDItem(toolboxDialog, toolboxDPeek^.editField + 1, itemType, itemHandle, itemRect);
  261.                 if PtInRect(localMouse, itemRect) then
  262.                     SetCursor(IBeam^^)
  263.                 else
  264.                     InitCursor
  265.             end
  266.     end;
  267.  
  268.  
  269.     procedure TDialog.Command (ch: Char; var itemHit: Integer);
  270.  
  271.     { Handle keyboard command. }
  272.  
  273.     begin
  274.         if toolboxDPeek^.editField <> -1 then
  275.             begin
  276.                 if ch in ['X', 'x'] then
  277.                     begin
  278.                         DlgCut(toolboxDialog);
  279.                         itemHit := toolboxDPeek^.editField + 1
  280.                     end
  281.                 else if ch in ['C', 'c'] then
  282.                     begin
  283.                         DlgCopy(toolboxDialog);
  284.                         itemHit := toolboxDPeek^.editField + 1
  285.                     end
  286.                 else if ch in ['V', 'v'] then
  287.                     begin
  288.                         DlgPaste(toolboxDialog);
  289.                         itemHit := toolboxDPeek^.editField + 1
  290.                     end
  291.                 else if ch in ['B', 'b'] then
  292.                     begin
  293.                         DlgDelete(toolboxDialog);
  294.                         itemHit := toolboxDPeek^.editField + 1
  295.                     end
  296.             end
  297.     end;
  298.  
  299.     procedure TDialog.Hit (var itemHit: Integer);
  300.  
  301.     { Special handling for item hit. }
  302.  
  303.     begin
  304.     end;
  305.  
  306.  
  307.     procedure TDialog.Draw;
  308.  
  309.     { Draw extra dialog embellishments. }
  310.  
  311.     begin
  312.         DrawDefault;
  313.     end;
  314.  
  315.  
  316.     procedure TDialog.DrawDefault;
  317.  
  318.     { Outline the default button. }
  319.  
  320.         var
  321.             itemType: Integer;
  322.             itemHandle: Handle;
  323.             itemRect, outlineRect: Rect;
  324.  
  325.     begin
  326.         if defaultItem > 0 then
  327.             begin
  328.                 GetDItem(toolboxDialog, defaultItem, itemType, itemHandle, itemRect);
  329.                 outlineRect := itemRect;
  330.                 InsetRect(outlineRect, -4, -4);
  331.                 PenSize(3, 3);
  332.                 FrameRoundRect(outlineRect, 16, 16);
  333.                 PenNormal
  334.             end
  335.     end;
  336.  
  337.  
  338.     procedure TDialog.DrawBox (itemSet: DialogItemSet; margin, thickness: Integer);
  339.  
  340.     { Draw a box around a set of dialog items. }
  341.  
  342.         var
  343.             itemType: Integer;
  344.             itemHandle: Handle;
  345.             itemRect, boxRect: Rect;
  346.             item: Integer;
  347.  
  348.     begin
  349.  
  350.         { Get the enclosing rectangle. }
  351.  
  352.         SetRect(boxRect, 0, 0, 0, 0);
  353.         for item := 1 to itemCount do
  354.             if item in itemSet then
  355.                 begin
  356.                     GetDItem(toolboxDialog, item, itemType, itemHandle, itemRect);
  357.                     if EmptyRect(boxRect) then
  358.                         boxRect := itemRect
  359.                     else
  360.                         UnionRect(boxRect, ItemRect, boxRect)
  361.                 end;
  362.  
  363.         { Draw the box. }
  364.  
  365.         if not EmptyRect(boxRect) then
  366.             begin
  367.                 InsetRect(boxRect, -(margin + thickness), -(margin + thickness));
  368.                 PenSize(thickness, thickness);
  369.                 FrameRect(boxRect);
  370.                 PenNormal
  371.             end
  372.     end;
  373.  
  374.  
  375.     procedure TDialog.DrawTitleBox (itemSet: DialogItemSet; margin, thickness: Integer; title: Str255);
  376.  
  377.     { Draws a box around a set of dialog items. }
  378.  
  379.         var
  380.             itemType: Integer;
  381.             itemHandle: Handle;
  382.             itemRect, boxRect: Rect;
  383.             item: Integer;
  384.             theInfo: FontInfo;
  385.  
  386.     begin
  387.  
  388.         { Get the enclosing rectangle. }
  389.  
  390.         SetRect(boxRect, 0, 0, 0, 0);
  391.         for item := 1 to itemCount do
  392.             if item in itemSet then
  393.                 begin
  394.                     GetDItem(toolboxDialog, item, itemType, itemHandle, itemRect);
  395.                     if EmptyRect(boxRect) then
  396.                         boxRect := itemRect
  397.                     else
  398.                         UnionRect(boxRect, ItemRect, boxRect)
  399.                 end;
  400.  
  401.         { Draw the box and the title. }
  402.  
  403.         if not EmptyRect(boxRect) then
  404.             begin
  405.                 GetFontInfo(theInfo);
  406.                 with boxRect, theInfo do
  407.                     begin
  408.                         left := left - margin - thickness;
  409.                         top := top - descent - margin - thickness;
  410.                         right := right + margin;
  411.                         bottom := bottom + margin;
  412.                         PenSize(thickness, thickness);
  413.                         MoveTo(left, top);
  414.                         Line(margin + 2, 0);
  415.                         Move(thickness + 1, (ascent + descent) div 2 - descent);
  416.                         DrawString(title);
  417.                         Move(1, -((ascent + descent) div 2 - descent));
  418.                         LineTo(right, top);
  419.                         LineTo(right, top);
  420.                         LineTo(right, bottom);
  421.                         LineTo(left, bottom);
  422.                         LineTo(left, top);
  423.                         PenNormal
  424.                     end
  425.             end
  426.     end;
  427.  
  428.  
  429.     procedure TDialog.FlashItem (item: Integer);
  430.  
  431.     { Hilight the dialog item for a few ticks. }
  432.  
  433.         var
  434.             itemType: Integer;
  435.             itemHandle: Handle;
  436.             itemRect: Rect;
  437.             finalTicks: Longint;
  438.  
  439.     begin
  440.         GetDItem(toolboxDialog, item, itemType, itemHandle, itemRect);
  441.         if BitAnd(itemType, ctrlItem) <> 0 then
  442.             begin
  443.                 HiliteControl(ControlHandle(itemHandle), ControlOn);
  444.                 Delay(FlashTicks, finalTicks);
  445.                 HiliteControl(ControlHandle(itemHandle), ControlOff);
  446.                 Delay(AfterTicks, finalTicks)
  447.             end
  448.     end;
  449.  
  450.  
  451.     function TDialog.GetText (item: Integer): Str255;
  452.  
  453.     { Get the dialog item text string. }
  454.  
  455.         var
  456.             itemType: Integer;
  457.             itemHandle: Handle;
  458.             itemRect: Rect;
  459.             text: Str255;
  460.  
  461.     begin
  462.         GetDItem(toolboxDialog, item, itemType, itemHandle, itemRect);
  463.         if BitAnd(itemType, statText + editText) <> 0 then
  464.             GetIText(itemHandle, text)
  465.         else
  466.             text := '';
  467.         GetText := text
  468.     end;
  469.  
  470.  
  471.     procedure TDialog.SetText (item: Integer; text: Str255);
  472.  
  473.     { Set the dialog item text string. }
  474.  
  475.         var
  476.             itemType: Integer;
  477.             itemHandle: Handle;
  478.             itemRect: Rect;
  479.  
  480.     begin
  481.         GetDItem(toolboxDialog, item, itemType, itemHandle, itemRect);
  482.         if BitAnd(itemType, statText + editText) <> 0 then
  483.             begin
  484.                 SetIText(itemHandle, text);
  485.                 if toolboxDPeek^.editField + 1 = item then
  486.                     SelIText(toolboxDialog, item, 0, 32767);
  487.             end
  488.     end;
  489.  
  490.  
  491.     function TDialog.GetInteger (item: Integer): Longint;
  492.  
  493.     { Get an integer value from the dialog item text. }
  494.  
  495.         var
  496.             text: Str255;
  497.             value: Longint;
  498.  
  499.     begin
  500.         text := GetText(item);
  501.         StringToNum(text, value);
  502.         GetInteger := value
  503.     end;
  504.  
  505.  
  506.     procedure TDialog.SetInteger (item: Integer; value: Longint);
  507.  
  508.     { Set the dialog item text string to represent an integer. }
  509.  
  510.         var
  511.             text: Str255;
  512.  
  513.     begin
  514.         NumToString(value, text);
  515.         SetText(item, text)
  516.     end;
  517.  
  518.  
  519.     function TDialog.GetReal (item: Integer): Extended;
  520.  
  521.     { Get a real value from the dialog item text. }
  522.  
  523.         var
  524.             text: Str255;
  525.             value: Extended;
  526.  
  527.     begin
  528.         text := GetText(item);
  529.         value := Str2Num(text);
  530.         GetReal := value
  531.     end;
  532.  
  533.  
  534.     procedure TDialog.SetReal (item: Integer; value: Extended; places: Integer);
  535.  
  536.     { Set the dialog item text string to represent a real number. }
  537.  
  538.         var
  539.             format: DecForm;
  540.             text: DecStr;
  541.  
  542.     begin
  543.         SetRound(ToNearest);
  544.         format.style := FixedDecimal;
  545.         format.digits := places;
  546.         Num2Str(format, value, text);
  547.         SetText(item, text)
  548.     end;
  549.  
  550.  
  551.     function TDialog.GetControlValue (item: Integer): Integer;
  552.  
  553.     { Get the control value. }
  554.  
  555.         var
  556.             itemType: Integer;
  557.             itemHandle: Handle;
  558.             itemRect: Rect;
  559.             value: Integer;
  560.  
  561.     begin
  562.         GetDItem(toolboxDialog, item, itemType, itemHandle, itemRect);
  563.         if BitAnd(itemType, ctrlItem) <> 0 then
  564.             value := GetCtlValue(ControlHandle(itemHandle))
  565.         else
  566.             value := 0;
  567.         GetControlValue := value
  568.     end;
  569.  
  570.  
  571.     procedure TDialog.SetControlValue (item: Integer; value: Integer);
  572.  
  573.     { Set the control value. }
  574.  
  575.         var
  576.             itemType: Integer;
  577.             itemHandle: Handle;
  578.             itemRect: Rect;
  579.  
  580.     begin
  581.         GetDItem(toolboxDialog, item, itemType, itemHandle, itemRect);
  582.         if BitAnd(itemType, ctrlItem) <> 0 then
  583.             SetCtlValue(ControlHandle(itemHandle), value)
  584.     end;
  585.  
  586.  
  587.     function TDialog.GetControlTitle (item: Integer): Str255;
  588.  
  589.     { Get the control title. }
  590.  
  591.         var
  592.             itemType: Integer;
  593.             itemHandle: Handle;
  594.             itemRect: Rect;
  595.             title: Str255;
  596.  
  597.     begin
  598.         GetDItem(toolboxDialog, item, itemType, itemHandle, itemRect);
  599.         if BitAnd(itemType, ctrlItem) <> 0 then
  600.             GetCTitle(ControlHandle(itemHandle), title)
  601.         else
  602.             title := '';
  603.         GetControlTitle := title
  604.     end;
  605.  
  606.  
  607.     procedure TDialog.SetControlTitle (item: Integer; title: Str255);
  608.  
  609.     { Set the control title. }
  610.  
  611.         var
  612.             itemType: Integer;
  613.             itemHandle: Handle;
  614.             itemRect: Rect;
  615.  
  616.     begin
  617.         GetDItem(toolboxDialog, item, itemType, itemHandle, itemRect);
  618.         if BitAnd(itemType, ctrlItem) <> 0 then
  619.             SetCTitle(ControlHandle(itemHandle), title)
  620.     end;
  621.  
  622.  
  623.     function TDialog.GetRadioItem (radioSet: DialogItemSet): Integer;
  624.  
  625.     { Get the selected radio button. }
  626.  
  627.         var
  628.             item, value: Integer;
  629.             itemSelected: Integer;
  630.  
  631.     begin
  632.         itemSelected := 0;
  633.         item := 1;
  634.         while (itemSelected = 0) and (item <= itemCount) do
  635.             begin
  636.                 if item in radioSet then
  637.                     if GetControlValue(item) = ControlOn then
  638.                         itemSelected := item;
  639.                 item := item + 1
  640.             end;
  641.         GetRadioItem := itemSelected
  642.     end;
  643.  
  644.  
  645.     procedure TDialog.SetRadioItem (radioSet: DialogItemSet; itemSelected: Integer);
  646.  
  647.     { Set the selected radio button. }
  648.  
  649.         var
  650.             item: Integer;
  651.  
  652.     begin
  653.         if itemSelected in radioSet then
  654.             for item := 1 to itemCount do
  655.                 if item in radioSet then
  656.                     if item = itemSelected then
  657.                         SetControlValue(item, ControlOn)
  658.                     else
  659.                         SetControlValue(item, ControlOff)
  660.     end;
  661.  
  662.  
  663.     procedure TDialog.SetIcon (item: Integer; iconID: Integer);
  664.  
  665.     { Set icon item. }
  666.  
  667.         var
  668.             theIcon: Handle;
  669.             itemType: Integer;
  670.             itemHandle: Handle;
  671.             itemRect: Rect;
  672.  
  673.     begin
  674.         theIcon := GetIcon(iconID);
  675.         GetDItem(toolboxDialog, item, itemType, itemHandle, itemRect);
  676.         if (BitAnd(itemType, iconItem) <> 0) and (theIcon <> nil) then
  677.             SetDItem(toolboxDialog, item, itemType, Handle(theIcon), itemRect)
  678.     end;
  679.  
  680.  
  681.     procedure TDialog.SetPicture (item: Integer; pictureID: Integer);
  682.  
  683.     { Set picture item. }
  684.  
  685.         var
  686.             thePicture: PicHandle;
  687.             itemType: Integer;
  688.             itemHandle: Handle;
  689.             itemRect: Rect;
  690.  
  691.     begin
  692.         thePicture := GetPicture(pictureID);
  693.         GetDItem(toolboxDialog, item, itemType, itemHandle, itemRect);
  694.         if (BitAnd(itemType, picItem) <> 0) and (thePicture <> nil) then
  695.             SetDItem(toolboxDialog, item, itemType, Handle(thePicture), itemRect)
  696.     end;
  697.  
  698.  
  699.     procedure TDialog.SetUserItem (item: Integer; userProc: ProcPtr);
  700.  
  701.     { Set the dialog user item procedure. }
  702.  
  703.         var
  704.             itemType: Integer;
  705.             itemHandle: Handle;
  706.             itemRect: Rect;
  707.  
  708.     begin
  709.         GetDItem(toolboxDialog, item, itemType, itemHandle, itemRect);
  710.         if BitAnd(itemType, userItem) <> 0 then
  711.             SetDItem(toolboxDialog, item, itemType, Handle(userProc), itemRect)
  712.     end;
  713.  
  714.  
  715.     procedure TAboutDialog.IAboutDialog;
  716.  
  717.     { Initialize about dialog. }
  718.  
  719.     begin
  720.         IDialog(AboutID, OKButton, 0, 0, [OKButton], true)
  721.     end;
  722.  
  723.  
  724.     function TAboutDialog.Show: Integer;
  725.  
  726.     { Show about dialog. }
  727.  
  728.         var
  729.             s: Str255;
  730.  
  731.     begin
  732.         NumToString(MaxBlock div 1024, s);
  733.         s := Concat(s, 'K ', GetText(3));
  734.         SetText(3, s);
  735.         Show := inherited Show
  736.     end;
  737.  
  738.  
  739.     procedure TMessageDialog.IMessageDialog (message: Str255; iconID: Integer);
  740.  
  741.     { Initialize message dialog. }
  742.  
  743.     begin
  744.         IDialog(MessageID, OKButton, 0, 0, [OKButton], true);
  745.         SetIcon(2, iconID);
  746.         SetMessage(message)
  747.     end;
  748.  
  749.  
  750.     procedure TMessageDialog.SetMessage (message: Str255);
  751.  
  752.     {Set message text string. }
  753.  
  754.     begin
  755.         SetText(3, message)
  756.     end;
  757.  
  758.  
  759.     procedure TStringDialog.IStringDialog (prompt, default: Str255);
  760.  
  761.     { Initialize string dialog. }
  762.  
  763.     begin
  764.         IDialog(StringID, OKButton, CancelButton, 4, [OKButton, CancelButton], true);
  765.         SetData(prompt, default)
  766.     end;
  767.  
  768.  
  769.     procedure TStringDialog.SetData (prompt, default: Str255);
  770.  
  771.     { Set prompt and default text strings. }
  772.  
  773.     begin
  774.         SetText(3, prompt);
  775.         SetText(4, default)
  776.     end;
  777.  
  778.  
  779.     procedure TStringDialog.GetData (var response: Str255);
  780.  
  781.     { Get string. }
  782.  
  783.     begin
  784.         response := GetText(4)
  785.     end;
  786.  
  787.  
  788.     procedure TYesNoDialog.IYesNoDialog (prompt: Str255; default: Integer);
  789.  
  790.     { Initialize Yes/No dialog. }
  791.  
  792.     begin
  793.         IDialog(YesNoID, default, 0, 0, [YesButton, NoButton], true);
  794.         SetPrompt(prompt)
  795.     end;
  796.  
  797.  
  798.     procedure TYesNoDialog.SetPrompt (prompt: Str255);
  799.  
  800.     { Set prompt text string. }
  801.  
  802.     begin
  803.         SetText(2, prompt)
  804.     end;
  805.  
  806.  
  807.     procedure TYesNoCancelDialog.IYesNoCancelDialog (prompt: Str255; default: Integer);
  808.  
  809.     { Initialize Yes/No/Cancel dialog. }
  810.  
  811.     begin
  812.         IDialog(YesNoCancelID, default, CancelButton, 0, [YesButton, NoButton, CancelButton], true);
  813.         SetPrompt(prompt)
  814.     end;
  815.  
  816.  
  817.     procedure TYesNoCancelDialog.SetPrompt (prompt: Str255);
  818.  
  819.     { Set prompt text string. }
  820.  
  821.     begin
  822.         SetText(4, prompt)
  823.     end;
  824.  
  825.  
  826.     procedure TMarginsDialog.IMarginsDialog (defaultMargins: MarginRecord);
  827.  
  828.     { Initialize page margins dialog. }
  829.  
  830.     begin
  831.         IDialog(PageMarginsID, OKButton, CancelButton, 5, [OKButton, CancelButton], true);
  832.         measureSet := [12, 13, 14];
  833.         SetData(defaultMargins)
  834.     end;
  835.  
  836.  
  837.     procedure TMarginsDialog.Draw;
  838.  
  839.     { Draw. }
  840.  
  841.     begin
  842.         DrawTitleBox(measureSet, 3, 1, 'Measure');
  843.         DrawDefault
  844.     end;
  845.  
  846.  
  847.     procedure TMarginsDialog.Hit (var itemHit: Integer);
  848.  
  849.     { Item hit. }
  850.         var
  851.             oldItem: Integer;
  852.             factor: Extended;
  853.             places: Integer;
  854.  
  855.     begin
  856.         if itemHit in measureSet then
  857.             begin
  858.  
  859.                 { Convert margin values to new units. }
  860.  
  861.                 oldItem := GetRadioItem(measureSet);
  862.                 if oldItem <> itemHit then
  863.                     begin
  864.                         if (oldItem = 12) and (itemHit = 13) then
  865.                             begin
  866.  
  867.                                 { Inches to cm. }
  868.  
  869.                                 factor := 2.54;
  870.                                 places := 2
  871.                             end
  872.                         else if (oldItem = 12) and (itemHit = 14) then
  873.                             begin
  874.  
  875.                                 { Inches to points. }
  876.  
  877.                                 factor := 72.0;
  878.                                 places := 0
  879.                             end
  880.                         else if (oldItem = 13) and (itemHit = 12) then
  881.                             begin
  882.  
  883.                                 { Cm to inches. }
  884.  
  885.                                 factor := 1.0 / 2.54;
  886.                                 places := 2
  887.                             end
  888.                         else if (oldItem = 13) and (itemHit = 14) then
  889.                             begin
  890.  
  891.                                 { cm to points. }
  892.  
  893.                                 factor := 1.0 / 2.54 * 72.0;
  894.                                 places := 0
  895.                             end
  896.                         else if (oldItem = 14) and (itemHit = 12) then
  897.                             begin
  898.  
  899.                                 { Points to inches. }
  900.  
  901.                                 factor := 1.0 / 72.0;
  902.                                 places := 2
  903.                             end
  904.                         else if (oldItem = 14) and (itemHit = 13) then
  905.                             begin
  906.  
  907.                                 { Points to cm. }
  908.  
  909.                                 factor := 1.0 / 72.0 * 2.54;
  910.                                 places := 2
  911.                             end;
  912.  
  913.                         SetReal(5, GetReal(5) * factor, places);
  914.                         SetReal(7, GetReal(7) * factor, places);
  915.                         SetReal(9, GetReal(9) * factor, places);
  916.                         SetReal(11, GetReal(11) * factor, places);
  917.                     end;
  918.  
  919.                 SetRadioItem(measureSet, itemHit)
  920.             end
  921.     end;
  922.  
  923.  
  924.     procedure TMarginsDialog.SetData (newMargins: MarginRecord);
  925.  
  926.     { Set the page margin values. }
  927.  
  928.         var
  929.             places: Integer;
  930.  
  931.     begin
  932.         case newMargins.measure of
  933.  
  934.             inches: 
  935.                 begin
  936.                     SetRadioItem(measureSet, 12);
  937.                     places := 2
  938.                 end;
  939.  
  940.             cm: 
  941.                 begin
  942.                     SetRadioItem(measureSet, 13);
  943.                     places := 2
  944.                 end;
  945.  
  946.             points: 
  947.                 begin
  948.                     SetRadioItem(measureSet, 14);
  949.                     places := 0
  950.                 end;
  951.  
  952.         end;
  953.  
  954.         SetReal(5, newMargins.top, places);
  955.         SetReal(7, newMargins.bottom, places);
  956.         SetReal(9, newMargins.left, places);
  957.         SetReal(11, newMargins.right, places)
  958.     end;
  959.  
  960.  
  961.     procedure TMarginsDialog.GetData (var theMargins: MarginRecord);
  962.  
  963.     { Get the page margin values. }
  964.  
  965.     begin
  966.         case GetRadioItem(measureSet) of
  967.  
  968.             12: 
  969.                 theMargins.measure := inches;
  970.  
  971.             13: 
  972.                 theMargins.measure := cm;
  973.  
  974.             14: 
  975.                 theMargins.measure := points;
  976.  
  977.         end;
  978.         theMargins.top := GetReal(5);
  979.         theMargins.bottom := GetReal(7);
  980.         theMargins.left := GetReal(9);
  981.         theMargins.right := GetReal(11)
  982.     end;
  983.  
  984.  
  985.     procedure TFontSizeDialog.IFontSizeDialog (defaultSize: Integer);
  986.  
  987.     { Initialize font size dialog. }
  988.  
  989.     begin
  990.         IDialog(FontSizeID, OKButton, CancelButton, 4, [OKButton, CancelButton], true);
  991.         SetData(defaultSize)
  992.     end;
  993.  
  994.  
  995.     procedure TFontSizeDialog.SetData (fontSize: Integer);
  996.  
  997.     { Set the font size. }
  998.  
  999.     begin
  1000.         SetInteger(4, fontSize)
  1001.     end;
  1002.  
  1003.  
  1004.     procedure TFontSizeDialog.GetData (var fontSize: Integer);
  1005.  
  1006.     { Get the font size. }
  1007.  
  1008.     begin
  1009.         fontSize := GetInteger(4)
  1010.     end;
  1011.  
  1012.  
  1013. end.